package com.fastjrun.db2doc.service.impl;

import cn.smallbun.screw.core.Configuration;
import cn.smallbun.screw.core.engine.EngineConfig;
import cn.smallbun.screw.core.engine.EngineFileType;
import cn.smallbun.screw.core.engine.EngineTemplateType;
import cn.smallbun.screw.core.execute.DocumentationExecuteExt;
import cn.smallbun.screw.core.process.ProcessConfig;
import com.fastjrun.db2doc.Db2DocConfig;
import com.fastjrun.db2doc.service.Db2DocService;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;

/** @author cuiyingfeng */
@Service("db2DocService")
public class Db2DocServiceImpl implements Db2DocService {

  @Override
  public String generatorDoc(
      DataSource dataSource, Db2DocConfig db2DocConfig, ProcessConfig processConfig) {
    // 生成文件配置
    EngineFileType engineFileType = EngineFileType.HTML;
    switch (db2DocConfig.getDocType()) {
      case "word":
        engineFileType = EngineFileType.WORD;
        break;
      case "md":
        engineFileType = EngineFileType.MD;
        break;
      default:
        break;
    }
    EngineConfig engineConfig =
        EngineConfig.builder()
            // 生成文件路径，自己mac本地的地址，这里需要自己更换下路径
            .fileOutputDir(db2DocConfig.getDataDir())
            // 打开目录
            .openOutputDir(false)
            // 文件类型
            .fileType(engineFileType)
            // 生成模板实现
            .produceType(EngineTemplateType.freemarker)
            .build();

    // 生成文档配置（包含以下自定义版本号、描述等配置连接）
    Configuration config =
        Configuration.builder()
            .version("1.0.0")
            .description("生成文档信息描述")
            .dataSource(dataSource)
            .engineConfig(engineConfig)
            .produceConfig(processConfig)
            .build();
    DocumentationExecuteExt documentationExecute=new DocumentationExecuteExt(config);
    // 执行生成
    documentationExecute.execute();

    return documentationExecute.getDocName();
  }
}
